package org.edu.modules.stu.zhsz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.edu.common.base.BasePageDto;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.utils.ConvertUtils;
import org.edu.common.base.BaseServiceImpl;
import org.edu.modules.stu.zhsz.entity.VeStuCaucusUser;
import org.edu.modules.stu.zhsz.entity.VeStuMoralActiveUser;
import org.edu.modules.stu.zhsz.mapper.VeStuCaucusMapper;
import org.edu.modules.stu.zhsz.entity.VeStuCaucus;
import org.edu.modules.stu.zhsz.dto.VeStuCaucusDto;
import org.edu.modules.stu.zhsz.service.VeStuCaucusService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.edu.modules.stu.zhsz.service.VeStuCaucusUserService;
import org.edu.modules.stu.zhsz.service.VeStuMoralActiveUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;



/**
 * 实现类
 */
@Service
public class VeStuCaucusServiceImpl extends BaseServiceImpl<VeStuCaucus, VeStuCaucusMapper> implements VeStuCaucusService {

    @Autowired
    private VeStuCaucusUserService userService;


    public IPage<VeStuCaucusDto> pageDto(BasePageDto page, @Param(Constants.WRAPPER) Wrapper queryWrapper){
        return mapper.pageDto(page,queryWrapper);
    }
    /**
    * 查询实体类
    */
    public VeStuCaucusDto findDtoById(String id) {
        VeStuCaucus entity = super.findById(id);
        VeStuCaucusDto dto = ConvertUtils.convertToDto(entity, VeStuCaucusDto.class);


        LambdaQueryWrapper<VeStuCaucusUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(VeStuCaucusUser::getStuId);
        lambdaQueryWrapper.eq(VeStuCaucusUser::getCId,dto.getId());
        List<VeStuCaucusUser> veJkyOrganizeUsers = userService.listByWrapper(lambdaQueryWrapper);
        List<String> userIds = new ArrayList<>();
        for (VeStuCaucusUser user: veJkyOrganizeUsers) {
            userIds.add(user.getStuId()+"");
        }
        dto.setUserIds(userIds);
        
        return dto;
    }

    /**
    * 新增或者修改
    */
    @Transactional
    public void insertOrUpdate(VeStuCaucusDto dto,Boolean isAdd){
        if(isAdd){
            super.insert(dto);
        }else{
            userService.deleteByPId(dto.getId());
            super.update(dto);
        }

        for (String userId:dto.getUserIds()) {
            VeStuCaucusUser moralProgUser = new VeStuCaucusUser();
            moralProgUser.setCId(dto.getId());
            moralProgUser.setStuId(Integer.parseInt(userId));
            userService.insert(moralProgUser);
        }
    }

    @Override
    public void importExcel(List<Map<String, Object>> mapList){
        for (Map map: mapList) {
            VeStuCaucusDto dto = new VeStuCaucusDto();
                   //         dto.setTitle ((String)map.get(0000));
                   //         dto.setStartTime ((String)map.get(0000));
                   //         dto.setEndTime ((String)map.get(0000));
                   //         dto.setGrade ((String)map.get(0000));
                   //         dto.setOrganization ((String)map.get(0000));
            
            super.insert(dto);
        }

    }

}
